/* XMLSend.java
 *
 * Project/Product: GWBS2009
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   严首骅	2009-5-31	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2009
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package client;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import system.base.ProductStorage;
import system.base.Warehouse;
import system.data.WarehousePageData;

import com.apep.connect.APEPRequest;
import com.apep.connect.APEPResponse;
import com.apep.connect.APEPServiceException;
import com.apep.connect.client.APEPClient;
import com.apep.framework2008.APEPException;
import com.apep.lib.xml.SampleElement;
import com.hitrust.gs.bean.GSAlignmentBean;
import com.hitrust.gs.bean.GSCountAndSumBean;
import com.hitrust.gs.bean.GSInHeadBean;
import com.hitrust.gs.bean.GSInSubBean;
import com.hitrust.gs.bean.GSManufactureBean;
import com.hitrust.gs.bean.GSManufactureCategoryBean;
import com.hitrust.gs.bean.GSOutHeadBean;
import com.hitrust.gs.bean.GSOutSubBean;
import com.hitrust.gs.bean.GSProviderBean;
import com.hitrust.gs.bean.GSWhStaffBean;
import com.hitrust.gs.business.GSBusAlignmentProcessor;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.vo.GSManufactureAndProvicerVo;
import com.hitrust.gs.vo.GSManufactureVO;

import framework.core.DBConnection;
import framework.core.LogWriter;
import framework.core.StringUtil;

public class XMLSend extends APEPClient {
    /**
     * 
     */
    private static final String FAILED = "发送失败";
    private static XMLSend instance = null;

    public static synchronized XMLSend getInstance() {

        if (instance == null) {
            instance = new XMLSend();
            return instance;
        }
        return instance;
    }

    private void addChileElement(SampleElement element, String elementName, String elementValue) {
        element.addChildElement(new SampleElement(elementName, elementValue));
    }

    public boolean isToRong() {
        boolean rst = false;
        String configFile = "rongAiRemotet";
        String tClientInfoXmlPath = sSystemConfigurator.getParam(configFile);
        if (tClientInfoXmlPath.equals("0")) {
            rst = true;
        }
        return rst;
    }

    public boolean exist(String actionName, String Remark) throws GSException {
        boolean ts = true;

        ArrayList exist = GSBusAlignmentProcessor.searchTaskIsExist(actionName, Remark);
        if (exist.size() == 0) {
            ts = false;
        }
        return ts;
    }

    public void intoAL(String KEYID, String CONTENT, String ACTIONNAME, String REMARK) throws GSException {
        GSAlignmentBean aGSAlignmentBean = new GSAlignmentBean();
        aGSAlignmentBean.setKEYID(KEYID);
        aGSAlignmentBean.setCONTENT(CONTENT);
        aGSAlignmentBean.setACTIONNAME(ACTIONNAME);
        aGSAlignmentBean.setREMARK(REMARK);
        aGSAlignmentBean.setFILEDTIMES("0");
        try {
            GSBusAlignmentProcessor.addTask(aGSAlignmentBean);
        } catch (GSException e) {
            e.printStackTrace();
        }
    }

    public String StringTo(String st){
        if(st==null||st.equals("")||st.length()==0){
            String zero = "0";
            return zero;
        }else{
            return st;
        }
        
        
    }
    public boolean rongRequest(String actionName, String dataName, SampleElement element) {
        boolean rstrong = false;
        try {
            APEPRequest tRequest = new APEPRequest();
            tRequest.setAction(actionName);
            tRequest.setData(element);
            APEPResponse response = RongAiClient.getInstance().sendToApep(tRequest);
            boolean returnSuccess = response.get("ResultCode").toString().equals("00");
            if (returnSuccess) {
                System.out.println("ResultCode:" + response.get("ResultCode"));
                LogWriter.info("ResultCode:" + response.get("ResultCode"));
                return true;
            }
        } catch (APEPServiceException e) {
            LogWriter.error("通讯异常，无法连接服务器, " + e.getMessage());
            System.out.println("通讯异常，无法连接服务器, " + e.getMessage());
        }
        return rstrong;
    }
    
    public String projectNum(String remark){
        if (remark.equals("") || remark.length() == 0 || remark == "null") {
            return "";
        } else {
            int begin = remark.indexOf("@@");
            int end = remark.indexOf("$$");
            int length = end - begin;
            String ProjectNum = remark.substring(begin + 2, length);
            return ProjectNum;
        }
    }
    // 添加产品分类-物资类别
    public boolean submitMaterialTypeSendXML(String actionName, String dataName,
            String billcode) throws APEPServiceException,UnsupportedEncodingException, GSException {
        GSManufactureCategoryBean tGSManufactureCategoryBean= GSBusAlignmentProcessor.searchMaterialType(billcode);
        return submitMaterialTypeSendXML(actionName, dataName, tGSManufactureCategoryBean);
    }
    // 添加产品分类-物资类别
    public boolean submitMaterialTypeSendXML(String actionName, String dataName,
            GSManufactureCategoryBean aManufactureCategoryBean) throws APEPServiceException,UnsupportedEncodingException, GSException {
        boolean rst = false;
        SampleElement element = new SampleElement(dataName);
        addChileElement(element, "TypeNum", RADataChange.changeCategory(aManufactureCategoryBean.getGs_manufacture_category_id()));
        addChileElement(element, "TypeName", aManufactureCategoryBean.getGs_manufacture_category_name());

        String CONTENT = FAILED;
        String REMARK = aManufactureCategoryBean.getGs_manufacture_category_id();

        boolean isExist = exist(actionName, REMARK);
        if (isExist) {
            rst = rongRequest(actionName, dataName, element);
            if (!rst) {
                GSBusAlignmentProcessor.delExistTask(actionName, REMARK);
                intoAL(dataName, CONTENT, actionName, REMARK);
            } else {
                GSBusAlignmentProcessor.delExistTask(actionName, REMARK);
            }
        } else {
            rst = rongRequest(actionName, dataName, element);
            if (!rst) {
                intoAL(dataName, CONTENT, actionName, REMARK);
            }
        }

        return rst;
    }
    // 添加产品-物资基本信息
    public boolean submitMaterialInfoSendXML(String actionName, String dataName, String id)
            throws APEPServiceException, GSException {
        GSManufactureVO vo =  GSBusAlignmentProcessor.searchManufacture(id);
        return submitMaterialInfoSendXML(actionName, dataName, vo);
    }
    
    
    // 添加产品-物资基本信息
    public boolean submitMaterialInfoSendXML(String actionName, String dataName, GSManufactureVO vo)
            throws APEPServiceException, GSException {
        boolean rst = false;
        SampleElement element = new SampleElement(dataName);
        addChileElement(element, "WhSelfNum", vo.gs_concentricity_id);
        addChileElement(element, "Name", vo.gs_manufacture_name);
        addChileElement(element, "Type", vo.gs_manufacture_category_id);
        addChileElement(element, "SpecModel", "规格-" + vo.gs_manufacture_spec + "|型号-" + vo.gs_manufacture_type);
        addChileElement(element, "Unit", vo.gs_quant_unit);
        addChileElement(element, "Brand", vo.gs_manufacture_brand);
        addChileElement(element, "ProducePlace", vo.gs_make_area);
        addChileElement(element, "GroupCode", "");
        addChileElement(element, "GroupName", "");
        addChileElement(element, "PlanPrice", vo.gs_planprice);
        addChileElement(element, "ProduceFactory", vo.gs_manufacturer_name);
        addChileElement(element, "ValidTime", vo.gs_quality_assure_period);
        String CONTENT = FAILED;
        String REMARK = vo.gs_concentricity_id;

        boolean isExist = exist(actionName, vo.gs_manufacture_id);
        if (isExist) {
            rst = rongRequest(actionName, dataName, element);
            if (!rst) {
                GSBusAlignmentProcessor.delExistTask(actionName, REMARK);
                intoAL(dataName, CONTENT, actionName, REMARK);
            } else {
                GSBusAlignmentProcessor.delExistTask(actionName, REMARK);
            }
        } else {
            rst = rongRequest(actionName, dataName, element);
            if (!rst) {
                intoAL(dataName, CONTENT, actionName, REMARK);
            }
        }
        return rst;
    }
    // 添加仓库-仓库信息
    public boolean submitWareHouseSendXML(String actionName, String dataName,String  id)
            throws APEPServiceException, GSException ,Exception{
        DBConnection con=DBConnection.open(true);
        boolean rs = false;
        try {
            Warehouse ware=Warehouse.fetchByCode(con, id);           
            WarehousePageData data = new WarehousePageData();
            data.setCode(ware.getCode());
            data.setName(ware.getName());
            rs = submitWareHouseSendXML(actionName, dataName, data);
            
        } catch (Exception e) {
           e.printStackTrace(); 
        }finally{
            if (con!=null) {
                con.close();
            }
        }
        return rs;
    }    
    // 添加仓库-仓库信息
    public boolean submitWareHouseSendXML(String actionName, String dataName, WarehousePageData data)
            throws APEPServiceException, GSException {
        boolean rst = false;
        SampleElement element = new SampleElement(dataName);
        addChileElement(element, "WareHouseNum", RADataChange.changeWarehouse(data.getCode()));
        addChileElement(element, "WareHouseName", data.getName());
        String CONTENT =FAILED;
        String REMARK = data.getCode();

        boolean isExist = exist(actionName, REMARK);
        if (isExist) {
            rst = rongRequest(actionName, dataName, element);
            if (!rst) {
                GSBusAlignmentProcessor.delExistTask(actionName, REMARK);
                intoAL(dataName, CONTENT, actionName, REMARK);
            } else {
                GSBusAlignmentProcessor.delExistTask(actionName, REMARK);
            }
        } else {
            rst = rongRequest(actionName, dataName, element);
            if (!rst) {
                intoAL(dataName, CONTENT, actionName, REMARK);
            }
        }
        return rst;
    }
    
    // 添加供应商-供应商信息
    public boolean submitProviderSendXML(String actionName, String dataName, String id)
            throws APEPServiceException, GSException {
        GSProviderBean tGSProviderBean ;
        boolean rs = false;
        try {
            tGSProviderBean = GSBusAlignmentProcessor.searchProvider(id);
            rs = submitProviderSendXML(actionName, dataName, tGSProviderBean);
        } catch (GSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return rs;
    }

    // 添加供应商-供应商信息
    public boolean submitProviderSendXML(String actionName, String dataName, GSProviderBean tGSProviderBean)
            throws APEPServiceException, GSException {
        boolean rst = false;
        SampleElement element = new SampleElement(dataName);
        addChileElement(element, "ProviderNum", tGSProviderBean.getGs_provider_id());
        addChileElement(element, "ProviderName", tGSProviderBean.getGs_provider_name());

        String CONTENT = FAILED;
        String REMARK = tGSProviderBean.getGs_provider_id();

        boolean isExist = exist(actionName, REMARK);
        if (isExist) {
            rst = rongRequest(actionName, dataName, element);
            if (!rst) {
                GSBusAlignmentProcessor.delExistTask(actionName, REMARK);
                intoAL(dataName, CONTENT, actionName, REMARK);
            } else {
                GSBusAlignmentProcessor.delExistTask(actionName, REMARK);
            }
        } else {
            rst = rongRequest(actionName, dataName, element);
            if (!rst) {
                intoAL(dataName, CONTENT, actionName, REMARK);
            }
        }

        return rst;
    }

    // 删除供应商-供应商信息
    public boolean submitProviderDeleteSendXML(String actionName, String dataName, String id)
            throws APEPServiceException {
        GSProviderBean tGSProviderBean ;
        boolean rs = false;
        try {
            tGSProviderBean = GSBusAlignmentProcessor.searchProvider(id);
            rs = submitProviderDeleteSendXML(actionName, dataName, tGSProviderBean);
        } catch (GSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return rs;
    }    
    
    // 删除供应商-供应商信息
    public boolean submitProviderDeleteSendXML(String actionName, String dataName, GSProviderBean tGSProviderBean)
            throws APEPServiceException {
        boolean rst = false;
        SampleElement element = new SampleElement(dataName);
        addChileElement(element, "ProviderNum", tGSProviderBean.getGs_provider_id());
        rst = rongRequest(actionName, dataName, element);
        if (!rst) {
            GSAlignmentBean aGSAlignmentBean = new GSAlignmentBean();
            aGSAlignmentBean.setKEYID(dataName);
            aGSAlignmentBean.setCONTENT(FAILED);
            aGSAlignmentBean.setREMARK(tGSProviderBean.getGs_provider_id());
            aGSAlignmentBean.setACTIONNAME(actionName);
            aGSAlignmentBean.setFILEDTIMES("0");
            try {
                GSBusAlignmentProcessor.addTask(aGSAlignmentBean);
            } catch (GSException e) {
                e.printStackTrace();
            }
        }
        return rst;
    }
    // 添加入库单信息
    public boolean submitInWhSendXML(String actionName, String dataName, SampleElement data)
            throws APEPServiceException, GSException {
        String billcode = data.getChildElement("in_bill_code").getTagValue();
        return submitInWhSendXML(actionName, dataName, billcode);
    }
    
    // 添加入库单信息
    public boolean submitInWhSendXML(String actionName, String dataName, String billcode)
            throws APEPServiceException, GSException {
        boolean rst = false;
        String SpecModel = "";
        String Unit = "";
        GSInHeadBean aGSInHeadBean = new GSInHeadBean();
        GSInSubBean aGSInSubBean = new GSInSubBean();
        GSWhStaffBean aGSWhStaffBean = new GSWhStaffBean();
        GSCountAndSumBean aGSCountAndSumBean = new GSCountAndSumBean();
        GSManufactureBean aGSManufactureBean = new GSManufactureBean();
        //String billcode = "";
      //  String billcode = data.getChildElement("in_bill_code").getTagValue();
        ArrayList tListHead = GSBusAlignmentProcessor.searchInHead(billcode);
        ArrayList tListSub = GSBusAlignmentProcessor.searchInSub(billcode);
        if (tListSub.size()==0 || tListHead.size() == 0) {
            return true;
        }
        aGSInHeadBean = (GSInHeadBean) tListHead.get(0);
        SampleElement element = new SampleElement(dataName);
        addChileElement(element, "InWhNum", aGSInHeadBean.getIN_BILL_CODE());
        String inWhType = null;
        if(aGSInHeadBean.getIN_TYPE().equals("1001")){
            inWhType = "01";
        }else if(aGSInHeadBean.getIN_TYPE().equals("1002")){
            inWhType = "03";
        }else if(aGSInHeadBean.getIN_TYPE().equals("1003")){
            inWhType = "04";
        }
        addChileElement(element, "InWhType", inWhType);
        addChileElement(element, "WhInNum", RADataChange.changeWarehouse(aGSInHeadBean.getWAREHOUSE_CODE()));
        addChileElement(element, "Provider", aGSInHeadBean.getPROVIDER_CODE());
        addChileElement(element, "ProviderName", aGSInHeadBean.getPROVIDER_NAME());
        addChileElement(element, "InDate", aGSInHeadBean.getIN_TIME());
        addChileElement(element, "MakeDate", aGSInHeadBean.getCREATE_TIME());
        addChileElement(element, "TransManNum", aGSInHeadBean.getCREATE_USER_ID());
        addChileElement(element, "TransManName", aGSInHeadBean.getCREATE_USER());
        doubleDateIn(tListSub);
        for (int i = 0; i < tListSub.size(); i++) {
            aGSInSubBean = (GSInSubBean) tListSub.get(i);
            aGSManufactureBean = GSBusAlignmentProcessor.searchInProduct(aGSInSubBean.getPRODUCT_ID());
            if(aGSManufactureBean==null){
                System.out.println("入库单缺少产品，产品ID:"+aGSInSubBean.getPRODUCT_ID()+"，请通知管理员！");
                LogWriter.info("入库单缺少产品，产品ID:"+  aGSInSubBean.getPRODUCT_ID() +"，请通知管理员！");
                return false;
            }
            ArrayList tList = GSBusAlignmentProcessor.searchProductCount(aGSInSubBean.getPRODUCT_ID(),aGSInHeadBean.getWAREHOUSE_CODE());

            if (tList.size() > 0) {
                aGSCountAndSumBean = (GSCountAndSumBean) tList.get(0);
            }
            ArrayList wfList = GSBusAlignmentProcessor.searchWarehouseStaff(aGSInSubBean.getPRODUCT_ID(),
                    aGSInHeadBean.getWAREHOUSE_CODE());
            if (wfList.size() > 0) {
                aGSWhStaffBean = (GSWhStaffBean) wfList.get(0);
            }
            SampleElement elementItem1 = new SampleElement("Material");
            addChileElement(elementItem1, "MaterialNum", aGSInSubBean.getPRODUCT_CODE());
            addChileElement(elementItem1, "MaterialName",  aGSManufactureBean.getGs_manufacture_name());
            if(aGSManufactureBean!=null){
                SpecModel = aGSManufactureBean.getGs_manufacture_spec();
                Unit = aGSManufactureBean.getGs_quant_unit();
            }
            addChileElement(elementItem1, "SpecModel", SpecModel);
            addChileElement(elementItem1, "Unit", Unit);
            addChileElement(elementItem1, "FactNumber", this.StringTo(aGSInSubBean.getPRODUCT_COUNT()));
            addChileElement(elementItem1, "FactPrice", this.StringTo(aGSInSubBean.getACTUAL_PRICE()));
            if(aGSInSubBean.getPRODUCT_MONEY()==null){
                aGSInSubBean.setPRODUCT_MONEY("0");
            }
            if(aGSCountAndSumBean.getCOUNT()==null){
                aGSCountAndSumBean.setCOUNT("0");
            }
            double FactSum = StringUtil.strToDouble(aGSInSubBean.getACTUAL_PRICE())*StringUtil.strToDouble(aGSInSubBean.getPRODUCT_COUNT());
            addChileElement(elementItem1, "FactSum", String.valueOf(FactSum));
            addChileElement(elementItem1, "Quantity", this.StringTo(aGSCountAndSumBean.getCOUNT()));
            addChileElement(elementItem1, "Sum", this.StringTo(aGSCountAndSumBean.getSUM()));
            addChileElement(elementItem1, "AcceptManNum", aGSWhStaffBean.getCODE());
            addChileElement(elementItem1, "AcceptManName", aGSWhStaffBean.getNAME());
            element.addChildElement(elementItem1);
        }
      String content = FAILED;
      String REMARK = aGSInHeadBean.getIN_BILL_CODE();

        boolean isExist = exist(actionName, REMARK);
        if (isExist) {
            rst = rongRequest(actionName, dataName, element);
            if (!rst) {
                GSBusAlignmentProcessor.delExistTask(actionName, REMARK);
                intoAL(dataName, content, actionName, REMARK);
            } else {
                GSBusAlignmentProcessor.delExistTask(actionName, REMARK);
            }
        } else {
            rst = rongRequest(actionName, dataName, element);
            if (!rst) {
                intoAL(dataName, content, actionName, REMARK);
            }
        }

        return rst;
    }

    /**
     * @param tListSub
     */
    private void doubleDateIn(ArrayList tListSub) {
        GSInSubBean aGSInSubBean;
        for(int l = 0; l < tListSub.size(); l++){
            aGSInSubBean = (GSInSubBean) tListSub.get(l);

            for(int y = 0;y<tListSub.size();y++){
                GSInSubBean  aGSInSubBean2 = (GSInSubBean) tListSub.get(y); 
                if(aGSInSubBean.getPRODUCT_CODE().equals(aGSInSubBean2.getPRODUCT_CODE())&&!aGSInSubBean.getID().equals(aGSInSubBean2.getID())){
                    float count = Float.parseFloat(aGSInSubBean.getPRODUCT_COUNT());
                    float count2 = Float.parseFloat(aGSInSubBean2.getPRODUCT_COUNT());
                    aGSInSubBean.setPRODUCT_COUNT(String.valueOf(count+count2));
                    tListSub.remove(y);
                }
            }
        }
    }




    // 添加出库单信息
    public boolean submitOutWhSendXML(String actionName, String dataName, SampleElement data)
            throws APEPServiceException, GSException {
        String billcode = data.getChildElement("out_bill_code").getTagValue();
        return sendOutBill(actionName, dataName,  billcode);
    }
    
    // 添加出库单信息
    public boolean submitOutWhSendXML(String actionName, String dataName, String billcode)
            throws APEPServiceException, GSException {
        return sendOutBill(actionName, dataName,  billcode);
    }
    
    // 重发出库单信息
    public boolean submitOutWhSendXML(String actionName, String dataName, String startDate,String endDate )
            throws APEPServiceException, GSException {

        return sendOutBill(actionName, dataName, startDate,endDate);
    }
    private boolean sendOutBill(String actionName, String dataName,  String startDate,String endDate) throws GSException {
        ArrayList tListHead = GSBusAlignmentProcessor.searchOutHead(startDate,endDate);
        for(int i =0;i<tListHead.size();i++){
            GSOutHeadBean aGSOutHeadBean = (GSOutHeadBean) tListHead.get(i);
            ArrayList tListSub = GSBusAlignmentProcessor.searchOutSub(aGSOutHeadBean.getOUT_BILL_CODE()); 
            reSendbill(actionName, dataName,  aGSOutHeadBean, tListSub);
        }
       
        return true;
    }
    /**
     * @param actionName
     * @param dataName
     * @param SpecModel
     * @param Unit
     * @param billcode
     * @return
     * @throws GSException
     */
    private boolean sendOutBill(String actionName, String dataName, String billcode) throws GSException {
        ArrayList tListHead = GSBusAlignmentProcessor.searchOutHead(billcode);
        ArrayList tListSub = GSBusAlignmentProcessor.searchOutSub(billcode);
        return sendbill(actionName, dataName,  tListHead, tListSub);
    }

    /**
     * @param actionName
     * @param dataName
     * @param SpecModel
     * @param Unit
     * @param tListHead
     * @param tListSub
     * @return
     * @throws GSException
     */
    private boolean sendbill(String actionName, String dataName,  ArrayList tListHead, ArrayList tListSub) throws GSException {
        boolean rst = false;
        String SpecModel = "";
        String Unit = "";
        
        GSOutHeadBean aGSOutHeadBean;
        GSOutSubBean aGSOutSubBean;
        if (tListSub.size()==0 || tListHead.size()==0) {
            return true;
        }
        aGSOutHeadBean = (GSOutHeadBean) tListHead.get(0);
        SampleElement element = new SampleElement(dataName);
        addChileElement(element, "OutWhNum", aGSOutHeadBean.getOUT_BILL_CODE());
        addChileElement(element, "RepairNum", aGSOutHeadBean.getREPAIR_BILL_CODE());
        addChileElement(element, "ProjectNum", projectNum(aGSOutHeadBean.getREMARK()));
        addChileElement(element, "OutDate", aGSOutHeadBean.getOUT_TIME());
        addChileElement(element, "MakeDate", aGSOutHeadBean.getCREATE_TIME());
        addChileElement(element, "DrawDeptNum", aGSOutHeadBean.getCUSTOMER_CODE());
        addChileElement(element, "DrawDeptName", aGSOutHeadBean.getCUSTOMER_NAME());
        addChileElement(element, "AssemblyNum", "");
        String outWhType = null;
        if(aGSOutHeadBean.getOUT_TYPE().equals("2001")){
            outWhType = "11";
        }else if(aGSOutHeadBean.getOUT_TYPE().equals("2002")){
            outWhType = "13";
        }else if(aGSOutHeadBean.getOUT_TYPE().equals("2003")){
            outWhType = "15";
        }
        addChileElement(element, "OutWhType", outWhType);
        addChileElement(element, "WareHouseNum", RADataChange.changeWarehouse(aGSOutHeadBean.getWAREHOUSE_CODE()));
        GSCountAndSumBean aGSCountAndSumBean = new GSCountAndSumBean();
        GSManufactureBean aGSManufactureBean = new GSManufactureBean();
        GSWhStaffBean aGSWhStaffBean = new GSWhStaffBean();
        doubleData(tListSub);
        for (int i = 0; i < tListSub.size(); i++) {
            aGSOutSubBean = (GSOutSubBean) tListSub.get(i);

            aGSManufactureBean = GSBusAlignmentProcessor.searchInProduct(aGSOutSubBean.getPRODUCT_ID());
            if(aGSManufactureBean==null){
                System.out.println("入库单缺少产品，产品ID:"+aGSOutSubBean.getPRODUCT_ID()+"，请通知管理员！");
                LogWriter.info("入库单缺少产品，产品ID:"+  aGSOutSubBean.getPRODUCT_ID() +"，请通知管理员！");
                return false;
            }
            ArrayList tList = GSBusAlignmentProcessor.searchProductCount(aGSOutSubBean.getPRODUCT_ID(),aGSOutHeadBean.getWAREHOUSE_CODE());

            if (tList.size() > 0) {
                aGSCountAndSumBean = (GSCountAndSumBean) tList.get(0);
            }
            ArrayList wfList = GSBusAlignmentProcessor.searchWarehouseStaff(aGSOutSubBean.getPRODUCT_ID(),
                    aGSOutHeadBean.getWAREHOUSE_CODE());
            if (wfList.size() > 0) {
                aGSWhStaffBean = (GSWhStaffBean) wfList.get(0);
            }
            SampleElement elementItem1 = new SampleElement("Material");
            addChileElement(elementItem1, "MaterialNum", aGSOutSubBean.getPRODUCT_CODE());
            addChileElement(elementItem1, "FactNumber", this.StringTo(aGSOutSubBean.getPRODUCT_COUNT()));
            addChileElement(elementItem1, "FactPrice", this.StringTo(aGSOutSubBean.getPRODUCT_PRICE()));
            addChileElement(elementItem1, "FactSum", this.StringTo(aGSOutSubBean.getPRODUCT_MONEY()));
            addChileElement(elementItem1, "Quantity", this.StringTo(aGSCountAndSumBean.getCOUNT()));
            addChileElement(elementItem1, "Sum", this.StringTo(aGSCountAndSumBean.getSUM()));
            addChileElement(elementItem1, "MaterialName", aGSManufactureBean.getGs_manufacture_name());
            if(aGSManufactureBean!=null){
                SpecModel = aGSManufactureBean.getGs_manufacture_spec();
                Unit = aGSManufactureBean.getGs_quant_unit();
            }
            addChileElement(elementItem1, "SpecModel", SpecModel);
            addChileElement(elementItem1, "Unit", Unit);
            addChileElement(elementItem1, "DrawManNum", aGSOutSubBean.getGET_CUSTOMER_STAFF_CODE());
            addChileElement(elementItem1, "DrawManName", aGSOutSubBean.getGET_CUSTOMER_STAFF_NAME());
            addChileElement(elementItem1, "AcceptManNum", aGSWhStaffBean.getCODE());
            addChileElement(elementItem1, "AcceptManName", aGSWhStaffBean.getNAME());
            element.addChildElement(elementItem1);
        }
 
        String CONTENT = FAILED;
        String REMARK = aGSOutHeadBean.getOUT_BILL_CODE();

        boolean isExist = exist(actionName, REMARK);
        if (isExist) {
            rst = rongRequest(actionName, dataName, element);
            if (!rst) {
                GSBusAlignmentProcessor.delExistTask(actionName, REMARK);
                intoAL(dataName, CONTENT, actionName, REMARK);
            } else {
                GSBusAlignmentProcessor.delExistTask(actionName, REMARK);
            }
        } else {
            rst = rongRequest(actionName, dataName, element);
            if (!rst) {
                intoAL(dataName, CONTENT, actionName, REMARK);
            }
        }

        return rst;
    }
    private boolean reSendbill(String actionName, String dataName,  GSOutHeadBean aGSOutHeadBean, ArrayList tListSub) throws GSException {
        boolean rst = false;
        String SpecModel = "";
        String Unit = "";
        

        GSOutSubBean aGSOutSubBean;
        SampleElement element = new SampleElement(dataName);
        addChileElement(element, "OutWhNum", aGSOutHeadBean.getOUT_BILL_CODE());
        addChileElement(element, "RepairNum", aGSOutHeadBean.getREPAIR_BILL_CODE());
        addChileElement(element, "ProjectNum", projectNum(aGSOutHeadBean.getREMARK()));
        addChileElement(element, "OutDate", aGSOutHeadBean.getOUT_TIME());
        addChileElement(element, "MakeDate", aGSOutHeadBean.getCREATE_TIME());
        addChileElement(element, "DrawDeptNum", aGSOutHeadBean.getCUSTOMER_CODE());
        addChileElement(element, "DrawDeptName", aGSOutHeadBean.getCUSTOMER_NAME());
        addChileElement(element, "AssemblyNum", "");
        String outWhType = null;
        if(aGSOutHeadBean.getOUT_TYPE().equals("2001")){
            outWhType = "11";
        }else if(aGSOutHeadBean.getOUT_TYPE().equals("2002")){
            outWhType = "13";
        }else if(aGSOutHeadBean.getOUT_TYPE().equals("2003")){
            outWhType = "15";
        }
        addChileElement(element, "OutWhType", outWhType);
        addChileElement(element, "WareHouseNum", RADataChange.changeWarehouse(aGSOutHeadBean.getWAREHOUSE_CODE()));
        GSCountAndSumBean aGSCountAndSumBean = new GSCountAndSumBean();
        GSManufactureBean aGSManufactureBean = new GSManufactureBean();
        GSWhStaffBean aGSWhStaffBean = new GSWhStaffBean();
        doubleData(tListSub);
        for (int i = 0; i < tListSub.size(); i++) {
            aGSOutSubBean = (GSOutSubBean) tListSub.get(i);

            aGSManufactureBean = GSBusAlignmentProcessor.searchInProduct(aGSOutSubBean.getPRODUCT_ID());
            if(aGSManufactureBean==null){
                System.out.println("入库单缺少产品，产品ID:"+aGSOutSubBean.getPRODUCT_ID()+"，请通知管理员！");
                LogWriter.info("入库单缺少产品，产品ID:"+  aGSOutSubBean.getPRODUCT_ID() +"，请通知管理员！");
                return false;
            }
            ArrayList tList = GSBusAlignmentProcessor.searchProductCount(aGSOutSubBean.getPRODUCT_ID(),aGSOutHeadBean.getWAREHOUSE_CODE());

            if (tList.size() > 0) {
                aGSCountAndSumBean = (GSCountAndSumBean) tList.get(0);
            }
            ArrayList wfList = GSBusAlignmentProcessor.searchWarehouseStaff(aGSOutSubBean.getPRODUCT_ID(),
                    aGSOutHeadBean.getWAREHOUSE_CODE());
            if (wfList.size() > 0) {
                aGSWhStaffBean = (GSWhStaffBean) wfList.get(0);
            }
            SampleElement elementItem1 = new SampleElement("Material");
            addChileElement(elementItem1, "MaterialNum", aGSOutSubBean.getPRODUCT_CODE());
            addChileElement(elementItem1, "FactNumber", this.StringTo(aGSOutSubBean.getPRODUCT_COUNT()));
            addChileElement(elementItem1, "FactPrice", this.StringTo(aGSOutSubBean.getPRODUCT_PRICE()));
            addChileElement(elementItem1, "FactSum", this.StringTo(aGSOutSubBean.getPRODUCT_MONEY()));
            addChileElement(elementItem1, "Quantity", this.StringTo(aGSCountAndSumBean.getCOUNT()));
            addChileElement(elementItem1, "Sum", this.StringTo(aGSCountAndSumBean.getSUM()));
            addChileElement(elementItem1, "MaterialName", aGSManufactureBean.getGs_manufacture_name());
            if(aGSManufactureBean!=null){
                SpecModel = aGSManufactureBean.getGs_manufacture_spec();
                Unit = aGSManufactureBean.getGs_quant_unit();
            }
            addChileElement(elementItem1, "SpecModel", SpecModel);
            addChileElement(elementItem1, "Unit", Unit);
            addChileElement(elementItem1, "DrawManNum", aGSOutSubBean.getGET_CUSTOMER_STAFF_CODE());
            addChileElement(elementItem1, "DrawManName", aGSOutSubBean.getGET_CUSTOMER_STAFF_NAME());
            addChileElement(elementItem1, "AcceptManNum", aGSWhStaffBean.getCODE());
            addChileElement(elementItem1, "AcceptManName", aGSWhStaffBean.getNAME());
            element.addChildElement(elementItem1);
        }
     
        
        String CONTENT = FAILED;
        String REMARK = aGSOutHeadBean.getOUT_BILL_CODE();

 
       intoAL(dataName, CONTENT, actionName, REMARK); 

        return true;
    }

    /**
     * @param tListSub
     */
    private void doubleData(ArrayList tListSub) {
        GSOutSubBean aGSOutSubBean;
        for(int l = 0; l < tListSub.size(); l++){
            aGSOutSubBean = (GSOutSubBean) tListSub.get(l);
            for(int y = 0;y<tListSub.size();y++){
                GSOutSubBean  aGSOutSubBean2 = (GSOutSubBean) tListSub.get(y); 
                if(aGSOutSubBean.getPRODUCT_CODE().equals(aGSOutSubBean2.getPRODUCT_CODE())&&!aGSOutSubBean.getID().equals(aGSOutSubBean2.getID())){
                    float count = Float.parseFloat(aGSOutSubBean.getPRODUCT_COUNT());
                    float money = Float.parseFloat(aGSOutSubBean.getPRODUCT_MONEY());
                    float count2 = Float.parseFloat(aGSOutSubBean2.getPRODUCT_COUNT());
                    float money2 = Float.parseFloat(aGSOutSubBean2.getPRODUCT_MONEY());
                    aGSOutSubBean.setPRODUCT_COUNT(String.valueOf(count+count2));
                    aGSOutSubBean.setPRODUCT_MONEY(String.valueOf(money+money2));
                    tListSub.remove(y);
                }
            }
        }
    }
}
